<?php

class Owner{

    protected $db;

    protected $accounts = array();

    //protected $interval;

    protected $intervalMeeting;
    protected $intervalCall;
    /**
     * 
     */
//    const INTERVAL_MEETING = 2;
//    const INTERVAL_CALL = 1;

    /**
     *
     * 86400 = onwership calculate in days
     * 3600 = onwership calculate in hours
     * 60 = onwership calculate in minutes
     * 1 = onwership calculate in secons
     */
    const TIME_DURATION = 86400;


    public function __construct(){
        global $sugar_config;
        
        $this->intervalMeeting = $sugar_config['ownership']['meeting'];
        $this->intervalCall = $sugar_config['ownership']['call'];

        $this->db = $GLOBALS['db'];
    }
    /**
     * 
     * @return boolean
     */
    public function checkAccountsOwner(){
        $meetingTime = $this->intervalMeeting * self::TIME_DURATION;
        $callTime = $this->intervalCall * self::TIME_DURATION;
        //Select all accounts where contract has been signed by Users
        $pendingAccountsSQL = "SELECT DISTINCT a.id FROM accounts a
            LEFT JOIN accounts_opportunities ac ON a.id = ac.account_id
            LEFT JOIN opportunities o ON ac.opportunity_id = o.id
            
            LEFT JOIN accounts_contacts a_c ON a_c.account_id = a.id
            LEFT JOIN meetings m ON m.parent_id = a_c.contact_id

            WHERE a.date_ascribe IS NOT NULL
            AND a.assigned_user_id IS NOT NULL
            AND a.deleted = 0
            AND(
                (
                    UNIX_TIMESTAMP(o.date_entered) - UNIX_TIMESTAMP(a.date_ascribe) <= ".$meetingTime."
                    AND a.assigned_user_id = o.created_by
                    AND o.date_entered >= a.date_ascribe
                    AND o.sales_stage = 'Closed Won'
                    AND o.deleted = 0
                    AND a.source_ascribe = 'meeting'
                )
                OR(
                    UNIX_TIMESTAMP(o.date_entered) - UNIX_TIMESTAMP(a.date_ascribe) <= ".$callTime."
                    AND a.assigned_user_id = o.created_by
                    AND o.date_entered >= a.date_ascribe
                    AND o.sales_stage = 'Closed Won'
                    AND o.deleted = 0
                    AND a.source_ascribe = 'call'
                )
                OR(
                    (UNIX_TIMESTAMP() - 7200) - UNIX_TIMESTAMP(a.date_ascribe) <= ".$meetingTime."
                     AND a.source_ascribe = 'meeting'
                  )
                OR (
                    (UNIX_TIMESTAMP() - 7200) - UNIX_TIMESTAMP(a.date_ascribe) <= ".$callTime."
                     AND a.source_ascribe = 'call'
                   )
                OR (
                     m.date_entered >= a.date_ascribe
                     AND a.assigned_user_id = m.created_by
                     AND m.deleted = 0
                     AND a.source_ascribe = 'call'
                     AND UNIX_TIMESTAMP(m.date_entered) - UNIX_TIMESTAMP(a.date_ascribe) <= ".$callTime."
                     AND (UNIX_TIMESTAMP() - 7200) - UNIX_TIMESTAMP(a.date_ascribe) <= ".$meetingTime."

                   )
              )";

        //echo $pendingAccountsSQL."<br><br>";
        //
        //Select all accounts, where assigned user id field, needs to be set to NULL
        $sql = "SELECT acc.* FROM accounts acc
            WHERE acc.deleted = 0
            AND acc.date_ascribe IS NOT NULL
            AND acc.assigned_user_id IS NOT NULL
            AND acc.id NOT IN ($pendingAccountsSQL)";
        //echo $sql."<br><br>";

        $result = $this->db->query($sql);
        while($row = $this->db->fetchByAssoc($result) ){
            $acc = new Account();
            $acc->populateFromRow($row);
            $this->accounts[] = $acc;
        }

        //new
        if(!empty($this->accounts)){
            foreach($this->accounts as $acc){
                $acc->fetched_row = $acc->toArray();//needs to be first
                if($acc->source_entered == "research_finished"){
                    $acc->source_entered = "research";
                }
                $acc->assigned_user_id = "";
                $acc->date_ascribe = "";
                $acc->source_ascribe = "";
                $acc->save();
                //echo $acc->name;
            }
        }

        return true;
    }

}

